package com.qf.blog.controller;

import com.qf.blog.commons.constant.BlogConstants;
import com.qf.blog.commons.constant.RedisKeys;
import com.qf.blog.commons.utils.Result;
import com.qf.blog.commons.utils.UserThreadLocal;
import com.qf.blog.dto.TokenDto;
import com.qf.blog.entity.Comment;
import com.qf.blog.service.CommentService;
import com.qf.blog.service.DiscussPostService;
import com.qf.blog.service.ReplyService;
import com.qf.blog.vo.CommVo;
import com.qf.blog.vo.ReplyVo;
import com.qf.blog.vo.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Set;


@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private DiscussPostService discussService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/detail")
    public String detail(Integer id, Model model) throws ParseException { // detial?id=10
        if (id != null) {
            Topic detail = discussService.detail(id);
            Set<String> range = redisTemplate.opsForSet().members(String.format(RedisKeys.USER_LIKE_KEY, BlogConstants.POST, id));
            //当前登录用户的ID
            TokenDto tokenDto = UserThreadLocal.get();
            Integer userId = tokenDto.getUserId();
            System.out.println(userId);
            detail.setLikeStatus(0);
            if (range != null) {
                for (String s : range) {
                    if (userId.equals(Integer.valueOf(s))) {
                        detail.setLikeStatus(1);
                        break;
                    }
                }
            }
            detail.setLikeCount(range == null ? 0 : range.size());
            model.addAttribute("topic", detail);
            List<CommVo> commentVoList = commentService.getCommentVoList(id);
            model.addAttribute("commVo", commentVoList);
            TokenDto userToken = UserThreadLocal.get();
            for (CommVo c : commentVoList) {
                if (userToken == null) {
                    Integer likeStatus = 0;
                    Integer likeCount = 0;
                    c.setLikeStatus(likeStatus);
                    c.setLikeCount(likeCount);
                }
                String likeKey = null;
                if (c.getCommentEntity().getEntityType() == 1) {
                    likeKey = String.format(RedisKeys.USER_LIKE_KEY, BlogConstants.POST, c.getId());
                    String o = redisTemplate.opsForValue().get(likeKey);
//                    o.equals()
                } else if (c.getCommentEntity().getEntityType() == 2) {
                    likeKey = String.format(RedisKeys.USER_LIKE_KEY, BlogConstants.COMMENT, c.getId());
                    String o = redisTemplate.opsForValue().get(likeKey);
                }
                Integer cId = c.getId();
                String userLikeKey = String.format(RedisKeys.USER_LIKE_KEY, BlogConstants.USER, cId);
                Boolean b = redisTemplate.hasKey(userLikeKey);
                if (b) {
                    String o = redisTemplate.opsForValue().get(userLikeKey);
                    Integer likeCount = Integer.valueOf(o);
                    c.setLikeCount(likeCount);
                }
            }
        }
        return "site/discuss-detail";
    }

    @RequestMapping("/add")
    @ResponseBody
//    public Result add (@RequestParam Integer entityType, @RequestParam Integer entityId, @RequestParam String
//            content){
    public Result add(ReplyVo replyVo) {
        Integer entityId = replyVo.getEntityId();
        Integer entityType = replyVo.getEntityType();
        String content = replyVo.getContent();
        if (entityId != null) {
            // 1.判断用户是否登录
            TokenDto userToken = UserThreadLocal.get();

            if (userToken == null) {
                return Result.error("请先登录");
            }
            // 2.把登录的用户Id封装到
            Comment comment = new Comment();
            comment.setCreateTime(new Date());
            comment.setEntityId(entityId);
            comment.setEntityType(entityType);
            comment.setTargetId(entityId);
            comment.setUserId(userToken.getUserId());
            comment.setContent(content);
            replyService.save(comment);
        }
//        return "redirect:/";
        return Result.ok();
    }

}